package service

import (
	"errors"
	"haiqiu-blog-go/dao"
	"haiqiu-blog-go/models"
	"haiqiu-blog-go/utils"
)

func Login(userName, password string) (*models.LoginRes, error) {
	password = utils.Md5Crypt(password, "haiqiu")
	user := dao.GetUserByUserNameAndPassword(userName, password)
	if user == nil {
		return nil, errors.New("账号或密码不正确")
	}
	var uid = user.Uid
	token, err := utils.Award(&uid)
	if err != nil {
		return nil, errors.New("生成token错误")
	}
	var userInfo models.UserInfo
	userInfo.Uid = user.Uid
	userInfo.UserName = user.UserName
	userInfo.Avatar = user.Avatar
	var data = &models.LoginRes{
		token,
		userInfo,
	}
	return data, nil
}
